import re
import requests
import os
import shutil
from util.logger import get_looger

logger = get_looger("汽车之家.log", __file__)

logger.info("爬虫启动......")

try:
    root = 'static/汽车之家/'
    if os.path.exists(root):
        shutil.rmtree(root)
    os.makedirs(root)
    url='https://cmsin.api.autohome.com.cn/home/getNewAllAuthorVideos?_appid=pc'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
        'referer':'https://www.autohome.com.cn/'

    }
    response = requests.get(url,headers=headers)
    for item in response.json()['result']:
        for i in item:
            title = i['title']
            title_sub = re.sub(r'[<>:"/\\|?*]', '', title)
            video = i['pcUrl']
            response_video = requests.get(video)
            with open(f"{root}{title_sub}.mp4", "wb") as f:
                f.write(response_video.content)
            img_url =i['img']
            img_response = requests.get(img_url)
            logger.info(f"开始爬取{title}")
            with open(f"{root}{title_sub}.png", "wb") as f:
                f.write(img_response.content)
            print(i)

except Exception as e:
    logger.error(e)
